package com.nsoft.server.service.daoImpl;

import java.util.List;

import javax.persistence.Query;

import com.nsoft.server.master.beans.AccountMaster;
import com.nsoft.server.service.dao.AccountDao;
import com.nsoft.thickclient.trackMove.StringHelper;
import com.nsoft.thickclient.trackMove.intr.account.Account;
import com.nsoft.thickclient.trackMove.intr.account.ThinAccount;

public class AccountDaoImpl extends AbstractMasterGenericJPADAO<AccountMaster, Integer>
		implements AccountDao {
	
	public List<ThinAccount> getAllThinAccounts(ThinAccount thinAccount) {
		String name =StringHelper.replace("*", "%", thinAccount.getName());
		StringBuffer sb = new StringBuffer();
		sb
				.append("select New com.nsoft.thickclient.trackMove.intr.account.ThinAccount"
						+ "(master.accNumId,master.accCharName) from com.nsoft.server.master.beans.AccountMaster"
						+ " master where 1=1 ");
		if (thinAccount.getName() != null
				&& !thinAccount.getName().equalsIgnoreCase("")
				&& !thinAccount.getName().equalsIgnoreCase(" ")) {
			sb.append("and  master.accCharName like '" + name + "'");
		}
		Query query = getEntityManager().createQuery(sb.toString());
		return query.getResultList();
	}
	

	public List<AccountMaster> searchAccount(Account account) {
		
		String name = "";
		StringBuffer sb = new StringBuffer();
		sb
				.append("select master "
						+ " from com.nsoft.server.master.beans.AccountMaster"
						+ " master where 1=1 ");
		if (account.getName() != null
				&& !account.getName().equalsIgnoreCase("")
				&& !account.getName().equalsIgnoreCase(" ")) {
			name =StringHelper.replace("*", "%", account.getName());
			sb.append("and  master.accCharName like '" + name + "'");
		}
		
		if (account.getNumber() != null
				&& !account.getNumber().equalsIgnoreCase("")
				&& !account.getNumber().equalsIgnoreCase(" ")) {
			name =StringHelper.replace("*", "%", account.getNumber());
			sb.append("and  master.accNumId like '" + name + "'");
		}
		
		/*if (account.get() != null
				&& !account.getNumber().equalsIgnoreCase("")
				&& !account.getTan().equalsIgnoreCase(" ")) {
			name =StringHelper.replace("*", "%", account.getTan());
			sb.append("and  master.partyCharTan like '" + name + "'");
		}
		
		if (account.getTin() != null
				&& !account.getTin().equalsIgnoreCase("")
				&& !account.getTin().equalsIgnoreCase(" ")) {
			name =StringHelper.replace("*", "%", account.getTin());
			sb.append("and  master.partyCharTin like '" + name + "'");
		}
		
		if (account.getExim() != null
				&& !account.getExim().equalsIgnoreCase("")
				&& !account.getExim().equalsIgnoreCase(" ")) {
			name =StringHelper.replace("*", "%", account.getExim());
			sb.append("and  master.partyCharExim like '" + name + "'");
		}*/
		
		Query query = getEntityManager().createQuery(sb.toString());
		return query.getResultList();
	}

}
